#!/bin/csh -f
if(! -f mktm.def) then
    echo "missing input file mktm.def"
    exit
endif
set hl=100
set flip=10
set vpin=1
set startfile=
set fstart=`awk '$1=="startfile"{print $2}' mktm.def`
set restart=`awk '$1=="restart"{print $2}' mktm.def`
set vpin=`awk '$1=="vpin"{print $2}' mktm.def`
set ham=`awk '$1=="ham"{print $2}' mktm.def`
set ti=`awk '$1=="tilings"{print $2}' mktm.def`
set erep=`awk '$1=="erep"{print $2}' mktm.def`
set rn=`awk '$1=="rootname"{print $2}' mktm.def`
set etoydir=`awk '$1=="etoydir"{print $2}' mktm.def`
set d04dir=`awk '$1=="d04dir"{print $2}' mktm.def`
set hl=`awk '$1=="d04loops"{print $2}' mktm.def`
set vl=`awk '$1=="d04vloops"{print $2}' mktm.def`
set hlini=`awk '$1=="d04loopsini"{print $2}' mktm.def`
set vlini=`awk '$1=="d04vloopsini"{print $2}' mktm.def`
set flip=`awk '$1=="d04flips"{print $2}' mktm.def`
set mdir=`pwd`
if($ham == "")set ham="plain"
if($flip == "")set flip=10
if($hl == "")set hl=1
if($hlini == "")set hlini=1
if($vl == "")set vl=1
if($vlini == "")set vlini=1
if($vpin == "")set vpin=1
if($d04dir == "")set d04dir=../../d04dir/1
if($etoydir == "")set etoydir=../../etoydir/1
if(! -d $etoydir) then
    echo "Cant find dir $etoydir"
    exit
else
    cd $etoydir
    set etoydir=`pwd`
    cd $mdir
endif
if(! -d $d04dir) then
    echo "Cant find dir $d04dir"
    exit
else
    cd $d04dir
    set d04dir=`pwd`
    cd $mdir
endif
if (-f $etoydir/lock) then
    echo -n "directory $etoydir is locked : "
    cat $etoydir/lock
    exit
endif
if (-f $d04dir/lock) then
    echo -n "directory $d04dir is locked : "
    cat $d04dir/lock
    exit
endif
if($fstart != "") then
    echo "STAGE 1: collecting monolayers in $d04dir:t"
    echo "$HOSTNAME $$" > RUNNING_INI
    echo $rn > $etoydir/lock
    echo $rn > $d04dir/lock
    echo "Starting from single tiling"
    cp $fstart $d04dir/x.inp
    cd $d04dir
    echo -n "entering d04dir $d04dir:t (ham=$ham)... "
    mkdeco deco.$ham > mkdeco.rep
    set sched=mc_schedule.$ham
    d04 -lf $sched -flip $flip -couple -hl $hlini -vl $vlini -pin $vpin x.inp > d04.rep
    echo " done"
    echo "STAGE 1: registering via toy energies in $etoydir:t"
    cd $etoydir
    cat $d04dir/tilings.out|ept_ > esta.rep
    sesta esta.rep > esta1.rep
    cat $d04dir/tilings.out|poperep esta1.rep > tout
    cp tout $mdir/$rn".M"
    cp pop.erep $mdir/M.ldef
    cd $mdir
    rm RUNNING_INI
    rm $etoydir/lock
    rm $d04dir/lock
endif
set tind=`awk '{print $1}' M.ldef`
echo "STAGE 2: collecting adjacent layers for $#tind collected monolayers"
@ j =0
echo "$HOSTNAME $$" >  RUNNING
echo $rn >> $etoydir/lock
echo $rn >> $d04dir/lock
if($restart != "") echo "restarting from layer $restart"
foreach i ($tind)
    @ j++
    if($restart != "" && $j < $restart) goto skiplayer
    echo -n $j" " >> RUNNING
    echo "FIXED LAYER $j (sampling possible bilayers) : "
    cat $rn".M" |poptil $i > tout
    multi2 tout 1,1,2 > x2.inp
    cp x2.inp $d04dir
    cd $d04dir
    echo -n "entering d04dir $d04dir:t (ham=$ham)... "
    mkdeco deco.$ham > mkdeco.rep
    set sched=mc_schedule.$ham
    d04 -lf $sched -flip $flip -couple -hl $hl -vl $vl -pin -1 x2.inp > d04.rep
    echo " done"
    cd $mdir
    if(! -d "L"$j) mkdir "L"$j
    tilesort $d04dir/tilings.out > sort
    tilestat2 sort
    cd $etoydir
    cat $mdir/sort.two | ept_ > $mdir/two.edat
    cat $mdir/sort.2nd | ept_ > $mdir/2nd.edat
    cd $mdir
    sesta two.edat > "L"$j/two.erep
    sesta 2nd.edat > "L"$j/2nd.erep
    mv sort.two "L"$j/two
    mv sort.2nd "L"$j/2nd
    mv two.edat "L"$j
    mv 2nd.edat "L"$j
    rm sort 
skiplayer:
end
rm RUNNING
rm $etoydir/lock
rm $d04dir/lock
mkrtm
mktm
set thdir=`awk '$1=="thdir"{print $2}' mktm.def`
if("$thdir" != "") then
    if(-d $thdir) then
	th2tminp
	th2tm
    endif
else
    th2tm -0
endif
##
